﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

using System.IO;
using System.Reflection;
using System.Globalization;

namespace Octopus.Logging.Impl.Text.Common
{
    public sealed class Converter
    {
        public static long ToFileSize(string argValue, long defaultValue)
        {
            if (argValue == null)
            {
                return defaultValue;
            }

            string s = argValue.Trim().ToUpper(CultureInfo.InvariantCulture);
            long multiplier = 1;
            int index;

            if ((index = s.IndexOf("KB")) != -1)
            {
                multiplier = 1024;
                s = s.Substring(0, index);
            }
            else if ((index = s.IndexOf("MB")) != -1)
            {
                multiplier = 1024 * 1024;
                s = s.Substring(0, index);
            }
            else if ((index = s.IndexOf("GB")) != -1)
            {
                multiplier = 1024 * 1024 * 1024;
                s = s.Substring(0, index);
            }
            if (s != null)
            {
                // Trin again to remove whitespace between the number and the size specifier
                s = s.Trim();

                try
                {
                    return long.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo) * multiplier;
                }
                catch (Exception e)
                {

                }
            }
            return defaultValue;
        }
    }
}
